//
//  UIViewController+NavItem.m
//  hotelbrt
//
//  Created by Ron on 5/8/14.
//  Copyright (c) 2014 HGG. All rights reserved.
//

#import "UIViewController+NavItem.h"
#import <objc/runtime.h>
#import <CoreImage/CoreImage.h>
static id leftBlockKey;
static id rightBlockKey;
static id right2BlockKey;

@implementation UIViewController (NavItem)

- (void)configNavigationBackAction:(voidBlock)action{
    [self configNavigationLeftItemWith:[UIImage imageNamed:BackButtonImageName] andAction:action];
}

- (void)configNavigationLeftItemWith:(id)object andAction:(voidBlock)action{
    [self configNavigationItemWith:object leftOrRight:YES andAction:action];
}
- (void)configNavigationRightItemWith:(id)object andAction:(voidBlock)action{
    [self configNavigationItemWith:object leftOrRight:NO andAction:action];
}

- (void)configNavigationLeftString:(NSString*)text textFont:(UIFont*)font andAction:(voidBlock)action{
    [self configNavigationItemString:text withFont:font leftOrRight:YES andAction:action];
}
-(void)configNavigationRightString:(NSString *)text textFont:(UIFont *)font textColor:(UIColor *)color andAction:(voidBlock)action
{
    [self configNavigationRightString:text textFont:font andAction:action withColor:color];
}

- (void)configNavigationRightString:(NSString*)text textFont:(UIFont*)font andAction:(voidBlock)action{
    [self configNavigationItemString:text withFont:font leftOrRight:NO andAction:action];
}
- (void)configNavigationRightString:(NSString*)text textFont:(UIFont*)font andAction:(voidBlock)action withColor:(UIColor*)color{
    [self configNavigationItemString:text withFont:font leftOrRight:NO andAction:action withColor:color];
}
- (void)configNavigationBarTintColor:(UIColor*)color{
    const CGFloat *designValues = CGColorGetComponents(color.CGColor);
    CGFloat *realValues = malloc( sizeof(CGFloat) * 3);
    for (int i = 0 ; i < 3; i++) {
        realValues[i] = (designValues[i] - 0.23) / 0.6;
    }
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:realValues[0] green:realValues[1] blue:realValues[2] alpha:1]];
}

- (void)configNavigationBarTitleAppearance{
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeZero;
    [self.navigationController.navigationBar setTintColor:DefaultTextColorOfNavigationBarTitle];
    NSDictionary *attributes = @{ NSFontAttributeName: DefaultFontOfNavigationBarTitle,
                                  NSForegroundColorAttributeName: DefaultTextColorOfNavigationBarTitle,
                                  NSShadowAttributeName : shadow};
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
}

- (void)configDefaultNavigationBarStyle{
    [self configNavigationBarTitleAppearance];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTranslucent:NO];
    UIImageView *hairlineView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    hairlineView.backgroundColor = RGB(225, 225, 225);
//   hairlineView.image = [UIImage imageWithColor:[UIColor clearColor]];
}

- (void)configNavigationBarLine:(BOOL)hidden{
    UIImageView *hairlineView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    [hairlineView setHidden:hidden];
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)configNavigationItemString:(NSString*)text withFont:(UIFont*)font leftOrRight:(BOOL)left andAction:(voidBlock)action withColor:(UIColor*)color{
    if (action) {
        objc_setAssociatedObject(self, left ? &leftBlockKey : &rightBlockKey,
                                 action,
                                 OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:text style:UIBarButtonItemStyleDone target:self action:left ? @selector(pressLeft:) : @selector(pressRight:)];
    
    NSDictionary * attributes = @{NSFontAttributeName: font};
    [barButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
    [barButtonItem setTintColor:color];
    if (left) {
        self.navigationItem.leftBarButtonItem = barButtonItem;
    }else {
        self.navigationItem.rightBarButtonItem = barButtonItem;
    }
}

-(void)configNavigationRightItem1:(NSString*)text1
                        textFont1:(UIFont*)font1
                       textColor1:(UIColor*)color1
                       andAction1:(voidBlock)action1
                            Item2:(NSString*)text2
                        textFont2:(UIFont*)font2
                       textColor2:(UIColor*)color2
                       andAction2:(voidBlock)action2
{
    if (action1) {
        objc_setAssociatedObject(self, &rightBlockKey,
                                 action1,
                                 OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    if (action2) {
        objc_setAssociatedObject(self, &rightBlockKey,
                                 action2,
                                 OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    //item1
    UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithTitle:text1 style:UIBarButtonItemStyleDone target:self action:@selector(pressRight:)];
    
    NSDictionary * attributes1 = @{NSFontAttributeName: font1, NSForegroundColorAttributeName:color1};
    [barButtonItem1 setTitleTextAttributes:attributes1 forState:UIControlStateNormal];
    
//    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//    
//    rightButton.titleLabel.text = @"fdsa";
    
    
    //item2
    UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithTitle:text2 style:UIBarButtonItemStyleDone target:self action:@selector(pressRight:)];
    
    NSDictionary * attributes2 = @{NSFontAttributeName: font2, NSForegroundColorAttributeName:color2};
    [barButtonItem2 setTitleTextAttributes:attributes2 forState:UIControlStateNormal];
    
    [self.tabBarController.navigationItem setRightBarButtonItems : [NSArray arrayWithObjects:barButtonItem1,barButtonItem2 , nil]];
    
//    self.navigationItem.rightBarButtonItems = barButtonItem;
}

- (void)configNavigationItemString:(NSString*)text withFont:(UIFont*)font leftOrRight:(BOOL)left andAction:(voidBlock)action{
//    NSCAssert([text isKindOfClass:[NSString class]], @"the text must be class of NSString");
    
    if (action) {
        objc_setAssociatedObject(self, left ? &leftBlockKey : &rightBlockKey,
                                 action,
                                 OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:text style:UIBarButtonItemStyleDone target:self action:left ? @selector(pressLeft:) : @selector(pressRight:)];
    
    NSDictionary * attributes = @{NSFontAttributeName: font};
    [barButtonItem setTitleTextAttributes:attributes forState:UIControlStateNormal];
    [barButtonItem setTintColor:DefaultTextColorOfNavigationItem];
    if (left) {
        self.navigationItem.leftBarButtonItem = barButtonItem;
    }else {
        self.navigationItem.rightBarButtonItem = barButtonItem;
    }
}
-(void)configNavigationLeftItemWithCustom:(id)object andAction:(voidBlock)action
{
    UIBarButtonItem *item = object;
    
    self.navigationItem.leftBarButtonItem = item;
    
    
}
- (void)configNavigationItemWith:(id)object leftOrRight:(BOOL)left andAction:(voidBlock)action{
    
//    NSCAssert([object isKindOfClass:[NSString class]] || [object isKindOfClass:[UIImage class]], @"the object must be class of NSString or UIImage");
    if ([object isKindOfClass:[UIImage class]]) {
        if (action) {
            objc_setAssociatedObject(self, left ? &leftBlockKey : &rightBlockKey,
                                     action,
                                     OBJC_ASSOCIATION_COPY_NONATOMIC);
        }
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[object imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:left ? @selector(pressLeft:) : @selector(pressRight:)];
        if (left) {
            self.navigationItem.leftBarButtonItem = barButtonItem;
        }else {
            self.navigationItem.rightBarButtonItem = barButtonItem;
        }
    }else {
        [self configNavigationItemString:object withFont:DefaultFontOfNavigationBarTitle leftOrRight:left andAction:action];
    }
}

- (void)configNavigationRightItemWithImages:(UIImage*)image :(UIImage*)image2 andAction:(voidBlock)action :(voidBlock)action2
{
    if (action) {
        objc_setAssociatedObject(self,&rightBlockKey,
                                 action,
                                 OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    if (action2) {
        objc_setAssociatedObject(self,&right2BlockKey,
                                 action2,
                                 OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    
    NSMutableArray *rightButtons = [[NSMutableArray alloc] init];
    UIBarButtonItem *barButtonItem;
    UIBarButtonItem *barButtonItem2;

    barButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(pressRight:)];
    [barButtonItem setTintColor:[UIColor redColor]];
    
    barButtonItem2 = [[UIBarButtonItem alloc] initWithImage:image2  style:UIBarButtonItemStyleDone target:self action:@selector(pressRight2:)];
    [barButtonItem2 setTintColor:[UIColor redColor]];
    [rightButtons addObject:barButtonItem2];
    [rightButtons addObject:barButtonItem];
    
    self.navigationItem.rightBarButtonItems =rightButtons;
}

#pragma mark -
#pragma mark -------------------- User Action ---------------------
- (void)pressLeft:(id)sender{
    voidBlock action = objc_getAssociatedObject(self, &leftBlockKey);
    if (action) {
        action();
    }
}

- (void)pressRight:(id)sender{
    voidBlock action = objc_getAssociatedObject(self, &rightBlockKey);
    if (action) {
        action();
    }
}

- (void)pressRight2:(id)sender{
    voidBlock action = objc_getAssociatedObject(self, &right2BlockKey);
    if (action) {
        action();
    }
}

//自定义返回白色按钮
- (void)settingLeftBarButtonItem{
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 9, 15.5);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtn_Click) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
}

- (void)leftBtn_Click{
    //默认返回上一层
    [self.navigationController popViewControllerAnimated:YES];
}

@end







